package cz.data.domain.visual.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.visual.mapstruct.ScreenMapper;
import cz.data.domain.visual.model.dto.ScreenDto;
import cz.data.domain.visual.model.entity.ScreenEntity;
import cz.data.domain.visual.model.query.ScreenQuery;
import cz.data.domain.visual.model.vo.ScreenVo;
import cz.data.domain.visual.service.ScreenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"可视化酷屏配置"})
@RestController
@RequestMapping("/screens")
public class ScreenController extends BaseController {

    @Autowired
    private ScreenService screenService;

    @Autowired
    private ScreenMapper screenMapper;

    @ApiOperation(value = "可视化酷屏配置-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<ScreenVo> getScreenById(@PathVariable String id) {
        ScreenEntity screenEntity = screenService.getScreenById(id);
        return new SimpleDataResponse<>(screenMapper.toVO(screenEntity));
    }

    @ApiOperation(value = "可视化酷屏配置-分页查询")
    @ApiImplicitParam(name = "screenQuery", value = "查询实体screenQuery", required = true, dataTypeClass = ScreenQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<ScreenVo>> getScreenPage(ScreenQuery screenQuery) {
        QueryWrapper<ScreenEntity> queryWrapper = new QueryWrapper<>();
        IPage<ScreenEntity> page = screenService.page(new Page<>(screenQuery.getPageNum(), screenQuery.getPageSize()), queryWrapper);
        List<ScreenVo> collect = page.getRecords().stream().map(screenMapper::toVO).collect(Collectors.toList());
        JsonPage<ScreenVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "可视化酷屏配置-新增")
    @ApiImplicitParam(name = "screen", value = "详细实体screen", required = true, dataType = "ScreenDto")
    @PostMapping()
    public SimpleDataResponse<ScreenVo> saveScreen(@RequestBody @Validated({ValidationGroups.Insert.class}) ScreenDto screen) {
        ScreenEntity screenEntity = screenService.saveScreen(screen);
        return new SimpleDataResponse<>(screenMapper.toVO(screenEntity));
    }

    @ApiOperation(value = "可视化酷屏配置-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "screen", value = "详细实体screen", required = true, dataType = "ScreenDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<ScreenVo> updateScreen(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ScreenDto screen) {
        ScreenEntity screenEntity = screenService.updateScreen(screen);
        return new SimpleDataResponse<>(screenMapper.toVO(screenEntity));
    }

    @ApiOperation(value = "可视化酷屏配置-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteScreenById(@PathVariable String id) {
        screenService.deleteScreenById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "可视化酷屏配置-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteScreenBatch(@PathVariable List<String> ids) {
        screenService.deleteScreenBatch(ids);
        return BaseResponse.success();
    }

    @ApiOperation(value = "可视化酷屏配置-复制")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @PostMapping("/copy/{id}")
    public BaseResponse copyScreen(@PathVariable String id) {
        screenService.copyScreen(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "可视化酷屏配置-构建")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @PutMapping("/build/{id}")
    public BaseResponse buildScreen(@PathVariable String id, @RequestBody ScreenDto screen) {
        screenService.buildScreen(screen);
        return BaseResponse.success();
    }
}
